import { SETUSERINFO,LOGOUT } from './actionTypes'

let defaultState = {

}

// 数据持久化
// 处理本地存储的用户信息
const userInfo = localStorage.getItem('userInfo')?JSON.parse(localStorage.getItem('userInfo')):''
defaultState.userInfo = userInfo
// 处理token
const token = localStorage.getItem('token')?JSON.parse(localStorage.getItem('token')):''
defaultState.token = token

let reducer = (state = defaultState,action) => {
  let newState = JSON.parse(JSON.stringify(state))
  switch (action.type){
    case SETUSERINFO:
      newState.userInfo = action.userInfo;
      newState.token = action.token
      break;
    case LOGOUT:
      newState.userInfo = {}
      newState.token = ''
      break;
    default: break
  }
  // console.log(newState);
  // 将处理完成的数据存入本地存储
  localStorage.setItem('userInfo',JSON.stringify(newState.userInfo))
  localStorage.setItem('token',JSON.stringify(newState.token))
  return newState
}

export default reducer